<template>
	<view>
		<view class="xzshdz" v-if="JSON.stringify(address) == '{}'">
			<view class="xzshdz_btn" @click="chooseaddress">
				请选择收货地址+
			</view>
		</view>
		<view class="xzshdz_detail" v-else @click="chooseaddress()">
			<view class="xzshdz_detail_row">
				<view class="xzshdz_detail_row_left">
						<view class="xzshdz_detail_row_left_title">
							收货人:
						</view>
						<view class="xzshdz_detail_row_left_right">
							{{address.userName}}
						</view>
				</view>
				<view class="xzshdz_detail_row_right">
					<view class="xzshdz_detail_row_right_nei">
						<view class="xzshdz_detail_row_left_title">
							电话:
						</view>
						<view class="xzshdz_detail_row_left_right">
							{{address.telNumber}}
						</view> 
					</view>
					<uni-icons type="forward" size="20"></uni-icons>
				</view>
				
			</view>
			<view class="dz">
				<view class="dz_title">
						收货地址:
				</view>
				<view class="dz_detail">
					{{detail}}
				</view>
			</view>
		</view>
		<!-- 底部边框线-->
		<image src="../../static/cart_border@2x.png" class="address"></image>
	</view>
</template>

<script>
	import {mapState,mapMutations} from "vuex"
	export default {
		name:"my-address",
		data() {
			return {
				// address:{}
			
			}
		},
		computed:{
			detail(){
				return this.address.provinceName + this.address.cityName +this.address.countyName+this.address.detailInfo;
			},
			...mapState("m_user",["address"])
		},
		methods:{
			...mapMutations("m_user",["updateAddress"]),
		async chooseaddress(){
			uni.authorize({
			    scope: 'scope.userLocation',
			     success:async ()=>{
			       const [err , succ] = await uni.chooseAddress().catch(err => err);
			       //如果授权成功的话
			       if(err == null && succ.errMsg == "chooseAddress:ok"){
			       	console.log(succ);
			       	//返回结果是一个数组
			       	this.updateAddress(succ);
			       }
			    },
			 fail:async ()=>{
					const [err2,confirmResult] =  await uni.showModal({
						title: '提示',
						content: '检车到你没有授权是否打开授权',
						confirmText:"确认",
						cancelText:"取消"
					});
					if(err2 ) return 
					console.log(confirmResult);
					if(confirmResult.cancel) return uni.$showMsg("您取消了授权");
					if(confirmResult.confirm) return uni.openSetting({
						success:(res)=>{
							if(!res.authSetting['scope.userLocation']){
								return uni.$showMsg("您取消了授权!");
							}
							if(res.authSetting['scope.userLocation']){
								return uni.$showMsg("授权成功!请选择收货地址");
							}
								 
								 
						}
					});
				}
			});
			//这种写法就是返回的是一个数组对象 ，第一个是 错误消息 第二种是消息
				// const [err , succ] = await uni.chooseAddress().catch(err => err);
				// //如果授权成功的话
				// if(err == null && succ.errMsg == "chooseAddress:ok"){
				// 	console.log(succ);
				// 	//返回结果是一个数组
				// 	this.updateAddress(succ);
				// }
				//如果授权不成功的话
				// if(err && err.errMsg == "chooseAddress:fail auth deny"){
				// 	//重新进行授权
						
				// 	this.reAuth();
				// }
			
					
				},
				// async reAuth(){
				// 	const [err2,confirmResult] =  await uni.showModal({
				// 		title: '提示',
				// 		content: '检车到你没有授权是否打开授权',
				// 		confirmText:"确认",
				// 		cancelText:"取消"
				// 	});
				// 	if(err2 ) return 
				// 	console.log(confirmResult);
				// 	if(confirmResult.cancel) return uni.$showMsg("您取消了授权");
				// 	if(confirmResult.comfirm) return console.log("需要处理");
				// }
				
			}
			
		}
</script>

<style lang="scss">
	.address {
		width: 100%;
		display: block;
		height: 5px;
	}
	.xzshdz{
		width: 100%;
		height: 200rpx;
		display: flex;
		justify-content:center;
		align-items: center;
	}
	.xzshdz_btn{
		width: 150px;
		height: 70rpx;
		border-radius: 4px;
		background-color: #1dab17;
		color: #fff;
	}
	.xzshdz_btn{
		text-align: center;
		line-height: 70rpx;
	}
	.xzshdz_detail_row{
		width: 100%;
		display:flex;
		justify-content: space-around;
		align-items: center;
		padding: 20rpx 0;
	}
	.xzshdz_detail_row>view{
		width: 50%;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 16px;
	}
	.xzshdz_detail_row{
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 16px;
	}
	.xzshdz_detail_row_left_right{
		margin-left: 10rpx;
	}
	.xzshdz_detail{
		padding: 0 10px;
	}
	.dz{
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 16px;
		color: #222222;
		margin-bottom: 10px;
	}
	.dz_detail{
		margin-left: 10rpx;
	}
	.xzshdz_detail_row .xzshdz_detail_row_right{
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex: 1;
	}
	.xzshdz_detail_row_right_nei{
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
	.dz_title{
		white-space: nowrap;
	}
</style>
